// 01矩阵
https://leetcode.cn/problems/01-matrix/description/
class Solution {
public:
    typedef pair<int,int> PII;
    int n,m;
    int _x[4] = {0, 0, 1, -1};
    int _y[4] = {1, -1, 0, 0};
    vector<vector<int>> updateMatrix(vector<vector<int>>& mat) 
    {
        queue<PII> q;
        n = mat.size();
        m = mat[0].size();
        vector<vector<int>> ret(n,vector<int>(m,-1));
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                if(mat[i][j] == 0)
                {
                    ret[i][j] = 0;
                    q.push(make_pair(i,j));
                }
            }
        }

        while(q.size())
        {
            PII top = q.front();
            q.pop();
            for(int i = 0; i < 4; i++)
            {
                int xpos = top.first + _x[i];
                int ypos = top.second + _y[i];                  
                if(xpos >= 0 && xpos < n && ypos >= 0 && ypos < m 
                 && ret[xpos][ypos] == -1)
                {  
                    ret[xpos][ypos] = ret[top.first][top.second] + 1;
                    q.push(make_pair(xpos,ypos));
                }
            }
        }
        return ret;
    }
};